package com.zhiyizhen.callnumber.net.util;

import android.content.Context;
import android.os.Handler;
import android.os.Looper;

import com.zhiyizhen.callnumber.net.exception.IResponseInterceptor;

public class NetworkUtils {

    private static Context mContext;
    private static Handler sMainHandler;
    private static IResponseInterceptor interceptor;
    private static boolean debug;

    private NetworkUtils() {
    }

    public static void init(Context context, IResponseInterceptor interceptor, boolean debug) {
        if (context == null) {
            throw new NullPointerException("Context is null");
        }
        mContext = context.getApplicationContext();
        NetworkUtils.interceptor = interceptor;
        NetworkUtils.debug = debug;
    }

    public static Handler getMainHandler() {
        synchronized (NetworkUtils.class) {
            if (sMainHandler == null) {
                sMainHandler = new Handler(Looper.getMainLooper());
            }
        }
        return sMainHandler;
    }

    public static Context getContext() {
        return mContext;
    }

    public static IResponseInterceptor getInterceptor() {
        return interceptor;
    }

    public static void setInterceptor(IResponseInterceptor interceptor) {
        NetworkUtils.interceptor = interceptor;
    }

    public static boolean isDebug() {
        return debug;
    }

    public static void setDebug(boolean debug) {
        NetworkUtils.debug = debug;
    }
}
